package com.xueyi.system.parcel.controller;

import com.xueyi.system.api.dict.domain.dto.SysDictDataDto;
import com.xueyi.system.api.dict.domain.query.SysDictDataQuery;
import com.xueyi.system.area.domain.dto.BaseAreaDto;
import com.xueyi.system.area.service.IBaseAreaService;
import com.xueyi.system.dict.service.ISysDictDataService;
import com.xueyi.system.inspection.domain.dto.InspectionDto;
import com.xueyi.system.inspection.domain.query.InspectionQuery;
import com.xueyi.system.inspection.service.IInspectionService;
import com.xueyi.system.parcel.domain.query.BaseParcelQuery;
import com.xueyi.system.parcel.domain.dto.BaseParcelDto;
import com.xueyi.system.parcel.service.IBaseParcelService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.plantManager.domain.dto.BasePlantManagerDto;
import com.xueyi.system.plantManager.domain.query.BasePlantManagerQuery;
import com.xueyi.system.plantManager.service.IBasePlantManagerService;
import com.xueyi.system.productionManager.domain.dto.BaseProductionManagerDto;
import com.xueyi.system.productionManager.domain.query.BaseProductionManagerQuery;
import com.xueyi.system.productionManager.service.IBaseProductionManagerService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 地块管理管理 业务处理
 *
 * @author yanhe
 */
@RestController
@RequestMapping("/parcel")
public class BaseParcelController extends BaseController<BaseParcelQuery, BaseParcelDto, IBaseParcelService> {



    @Autowired
    private IBaseParcelService baseParcelService;

    @Autowired
    private IBasePlantManagerService basePlantManagerService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IBaseProductionManagerService baseProductionManagerService;

    @Autowired
    private IInspectionService iInspectionService;

    @Autowired
    private IBaseAreaService baseAreaService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "地块管理" ;
    }

    /**
     * 查询地块管理列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_PARCEL_LIST)
    public AjaxResult list(BaseParcelQuery parcel) {
        return super.list(parcel);
    }

    /**
     * 查询地块管理详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_PARCEL_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 地块管理导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_PARCEL_EXPORT)
    @Log(title = "地块管理管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BaseParcelQuery parcel) {
        super.export(response, parcel);
    }

    /**
     * 地块管理新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_PARCEL_ADD)
    @Log(title = "地块管理管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BaseParcelDto parcel) {

        if(getTotalArea(parcel.getAreaId())){
            return AjaxResult.error("地块面积大于基地面积，请确认下");
        }
        return super.add(parcel);
    }

    /**
     * 地块管理修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_PARCEL_EDIT)
    @Log(title = "地块管理管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BaseParcelDto parcel) {

        if(getTotalArea(parcel.getAreaId())){
            return AjaxResult.error("地块面积大于基地面积，请确认下");
        }
        return super.edit(parcel);
    }

    /**
     * 地块管理修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.BASE_PARCEL_EDIT, Auth.BASE_PARCEL_ES}, logical = Logical.OR)
    @Log(title = "地块管理管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody BaseParcelDto parcel) {
        return super.editStatus(parcel);
    }

    /**
     * 地块管理批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_PARCEL_DEL)
    @Log(title = "地块管理管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取地块管理选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    /**
     * 根据基地id查询对于的地块
     */

    @GetMapping("/getParcelList")
    public AjaxResult getParcelList(String areaId,String name) {
        BaseParcelQuery parcelQuery = new BaseParcelQuery();
        parcelQuery.setAreaId(areaId);
        if(!"".equals(name) && name != null){
            parcelQuery.setName(name);
        }
        parcelQuery.setStatus("1");
        List<BaseParcelDto> dtoList = baseParcelService.selectList(parcelQuery);
        for (BaseParcelDto parcelDto : dtoList) {
            BasePlantManagerQuery query = new BasePlantManagerQuery();
            List<BasePlantManagerDto> plantManagerDtos = basePlantManagerService.selectList(query);
            if(plantManagerDtos.size() > 0){
                BasePlantManagerDto plantManagerDto = plantManagerDtos.get(0);
                SysDictDataQuery dataQuery = new SysDictDataQuery();
                dataQuery.setCode("base_plant_type");
                dataQuery.setValue(plantManagerDto.getPlantType());
                List<SysDictDataDto> dataDtoList = dictDataService.selectList(dataQuery);
                if(dataDtoList.size() > 0){
                    parcelDto.setProperty(dataDtoList.get(0).getLabel());
                }
            }
        }
        return AjaxResult.success(dtoList);
    }

    /**
     * 农事记录
     */
    @GetMapping("/getFarmRecordList")
    public AjaxResult getFarmRecordList(String areaId){

        BaseProductionManagerQuery managerQuery = new BaseProductionManagerQuery();
        managerQuery.setBelongingId(areaId);
        List<BaseProductionManagerDto> managerDtoList = baseProductionManagerService.selectList(managerQuery);
        return  AjaxResult.success(managerDtoList);
    }

    /**
     * 巡检记录
     */
    @GetMapping("/getInspectionByList")
    public AjaxResult getInspectionByList(String areaId){

        InspectionQuery query = new InspectionQuery();
        query.setAreaId(areaId);
        List<InspectionDto> inspectionDtoList = iInspectionService.selectList(query);

        return AjaxResult.success(inspectionDtoList);
    }


    /**
     * 比较基地和地块的面积大小
     * @param areaId
     * @return
     */
    public Boolean getTotalArea(String areaId){

        Boolean result = false;

        BaseParcelQuery parcelQuery = new BaseParcelQuery();
        parcelQuery.setAreaId(areaId);
        parcelQuery.setStatus("1");
        List<BaseParcelDto> list = baseParcelService.selectList(parcelQuery);
        BigDecimal pacelTaol = new BigDecimal("0");
        for (BaseParcelDto parcelDto : list) {
            pacelTaol = pacelTaol.add(parcelDto.getArea());
        }
        BaseAreaDto baseAreaDto = baseAreaService.selectById(Long.valueOf(areaId));
        if(pacelTaol.compareTo(baseAreaDto.getArea()) == 1){
            result = true;
        }

        return result;
    }

    interface Auth {
        /** 系统 - 地块管理管理 - 列表 */
        String BASE_PARCEL_LIST = "system:parcel:list" ;
        /** 系统 - 地块管理管理 - 详情 */
        String BASE_PARCEL_SINGLE = "system:parcel:single" ;
        /** 系统 - 地块管理管理 - 新增 */
        String BASE_PARCEL_ADD = "system:parcel:add" ;
        /** 系统 - 地块管理管理 - 修改 */
        String BASE_PARCEL_EDIT = "system:parcel:edit" ;
        /** 系统 - 地块管理管理 - 修改状态 */
        String BASE_PARCEL_ES = "system:parcel:es" ;
        /** 系统 - 地块管理管理 - 删除 */
        String BASE_PARCEL_DEL = "system:parcel:delete" ;
        /** 系统 - 地块管理管理 - 导出 */
        String BASE_PARCEL_EXPORT = "system:parcel:export" ;
    }
}
